package com.jddup.program.comparator;

import com.jddup.common.pojo.TableColumn;
import com.jddup.common.pojo.TableMeta;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by zhangzhenbin on 17-12-11.
 */
public interface CreateDDLBuilder extends ColumnSqlBuilder {
    String getTemplateSQL();

    default String build(TableMeta tableMeta) {
        List<TableColumn> tableColumns = tableMeta.getTableColumns();
        StringBuilder columnDDL = new StringBuilder();
        for (TableColumn tableColumn : tableColumns) {
            buildColumnSql(tableColumn, columnDDL);
            columnDDL.append(",");
        }
        if (columnDDL.length() > 0) {
            columnDDL.deleteCharAt(columnDDL.length() - 1);
        }
        return getTemplateSQL()
                .replace("{tableName}", tableMeta.getTableName())
                .replace("{columns}", columnDDL)
                .replace("{tableComment}", tableMeta.getTableComment().
                        replace("'", "").
                        replace("\"", "").
                        replace(" ", "").
                        replace("\n", ""));
    }
}
